﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;

public enum ActionType {
    NA = 0,
    power,
    attack,
    defence,
    skill
}

public class DemoPlayer : MonoBehaviour {
    public Battle _Battle;

	public Text TextHp;
	public Text TextMp;

    public Image HpSlot;
    public Image MpSlot;

    public int maxHp = 5;
    public int maxMp = 5;

    public int hp = 5;
    public int mp = 2;

    public bool isPlayer = true;

    public bool isReady = false;

    public Button BtnPower;
    public Button BtnAttack;
    public Button BtnDefence;
    public Button BtnSuperSkill;

    // Use this for initialization
    void Start() {
        if(isPlayer) {
            BtnPower.onClick.AddListener(OnBtnPowerClick);
            BtnAttack.onClick.AddListener(OnBtnAttackClick);
            BtnDefence.onClick.AddListener(OnBtnDefenceClick);
            BtnSuperSkill.onClick.AddListener(OnBtnSuperSkillClick);
        }
        else {
           UnityEngine.Random.InitState(DateTime.Now.Millisecond);
        }
    }

    public ActionType actionType = ActionType.NA;

    public void OnBtnPowerClick() {
        isReady = true;
        actionType = ActionType.power;
    }

    public void OnBtnAttackClick() {
        isReady = true;
        actionType = ActionType.attack;
    }

    public void OnBtnDefenceClick() {
        isReady = true;
        actionType = ActionType.defence;
    }

    public void OnBtnSuperSkillClick() {
        isReady = true;
        actionType = ActionType.skill;
    }

	private void SetHpMpText() {
		TextHp.text = "Hp " + hp;
		TextMp.text = "Mp " + mp;
	}
    // Update is called once per frame
    void Update() {
        bool powerEnable = _Battle.isWaitAction && !isReady && mp < maxMp;
        bool attackEnable = _Battle.isWaitAction && !isReady && mp > 0;
        bool defenceEnable = _Battle.isWaitAction && !isReady;
        bool skillEnable = _Battle.isWaitAction && !isReady && mp >= 3;
        if(isPlayer) {
            BtnPower.enabled = powerEnable;
            BtnAttack.enabled = attackEnable;
            BtnDefence.enabled = defenceEnable;
            BtnSuperSkill.enabled = skillEnable;
        }
        else {
            if(_Battle.isWaitAction && !isReady) {
                List<ActionType> randomList = new List<ActionType>();
                if (powerEnable)
                    randomList.Add(ActionType.power);
                if (attackEnable)
                    randomList.Add(ActionType.attack);
                if (defenceEnable && _Battle.player.mp > 0)
                    randomList.Add(ActionType.defence);
                if (skillEnable)
                    randomList.Add(ActionType.skill);
                int index = UnityEngine.Random.Range(0, randomList.Count);

                switch(randomList[index]) {
                    case ActionType.power:
                        OnBtnPowerClick();
                        break;
                    case ActionType.attack:
                        OnBtnAttackClick();
                        break;
                    case ActionType.defence:
                        OnBtnDefenceClick();
                        break;
                    case ActionType.skill:
                        OnBtnSuperSkillClick();
                        break;
                }
            }
        }
		SetHpMpText();
    }
}
